﻿namespace ThingsGateway.Foundation
{
    public class Result<T> : Result
    {

        #region Public Constructors

        public Result()
        {

        }
        public Result(T value, string msg = "")
        {
            Content = value;
            Message = msg;
        }

        public Result(ResultCode resultCode, string msg = "") : base(resultCode, msg) { }

        public Result(string msg) : base(msg) { }
        #endregion Public Constructors

        #region Public Properties

        public T Content { get; set; }

        #endregion Public Properties
        public Result<TResult> Then<TResult>(Func<T, Result<TResult>> func)
        {
            return !IsSuccess ? Result.CreateFailedResult<TResult>(this) : func(Content);
        }

    }
    public class Result<T1, T2> : Result
    {
        public Result()
        {

        }
        public Result(T1 t1, T2 t2)
        {
            Content1 = t1;
            Content2 = t2;
        }
        public Result(ResultCode resultCode, string msg = "") : base(resultCode, msg) { }

        public Result(string msg) : base(msg) { }
        #region Public Properties

        public T1 Content1 { get; set; }
        public T2 Content2 { get; set; }

        #endregion Public Properties

    }
    public class Result : IResult
    {

        #region Public Constructors

        public Result()
        {
        }

        public Result(ResultCode resultCode, string msg = "")
        {
            ResultCode = resultCode;
            Message = msg;
        }

        public Result(string msg)
        {
            Message = msg;
        }

        #endregion Public Constructors

        #region Public Properties

        public bool IsSuccess => ResultCode.HasFlag(ResultCode.Success);

        public string Message { get; set; } = Resource.UnknownError;

        public ResultCode ResultCode { get; set; } = ResultCode.Default;

        #endregion Public Properties

        #region Public Methods

        public static Result CreateSuccessResult()
        {
            return new Result()
            {
                ResultCode = ResultCode.Success,
                Message = Resource.SuccessText
            };
        }
        public static Result<T> CreateSuccessResult<T>(T value)
        {
            return new Result<T>()
            {
                ResultCode = ResultCode.Success,
                Message = Resource.SuccessText,
                Content = value
            };
        }
        public static Result<T1, T2> CreateSuccessResult<T1, T2>(T1 value1, T2 value2)
        {
            return new Result<T1, T2>()
            {
                ResultCode = ResultCode.Success,
                Message = Resource.SuccessText,
                Content1 = value1,
                Content2 = value2
            };
        }

        #endregion Public Methods
        /// <summary>创建并返回一个失败的结果对象，该对象复制另一个结果对象的错误信息</summary>
        /// <typeparam name="T1">目标数据类型</typeparam>
        /// <param name="result">之前的结果对象</param>
        /// <returns>带默认泛型对象的失败结果类</returns>
        public static Result<T1> CreateFailedResult<T1>(Result result)
        {
            Result<T1> failedResult = new Result<T1>
            {
                ResultCode = result.ResultCode,
                Message = result.Message,
            };
            return failedResult;
        }

        /// <summary>创建并返回一个失败的结果对象，该对象复制另一个结果对象的错误信息</summary>
        /// <typeparam name="T1">目标数据类型一</typeparam>
        /// <typeparam name="T2">目标数据类型二</typeparam>
        /// <param name="result">之前的结果对象</param>
        /// <returns>带默认泛型对象的失败结果类</returns>
        public static Result<T1, T2> CreateFailedResult<T1, T2>(Result result)
        {
            Result<T1, T2> failedResult = new Result<T1, T2>
            {
                ResultCode = result.ResultCode,
                Message = result.Message
            };
            return failedResult;
        }

    }
}